package com.atguigu.tingshu.user.service.impl;

import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserPaidAlbumMapper userPaidAlbumMapper;
    @Autowired
    private UserPaidTrackMapper userPaidTrackMapper;

    /**
     * 根据用户id获取用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public UserInfoVo getUserInfoVoByUserId(Long userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);
        return userInfoVo;
    }

    /**
     * 判断用户是否购买声音列表
     *
     * @param albumId
     * @param trackIdList
     * @return
     */
    @Override
    public Map<Long, Integer> userIsPaidTrack(Long albumId, List<Long> trackIdList, Long userId) {
        //	map.put(trackId,1): 当前声音已购买，map.put(trackId,0): 当前声音未购买
        //	先判断当前这个用户是否购买过专辑; user_paid_album
        UserPaidAlbum userPaidAlbum = userPaidAlbumMapper.selectOne(new LambdaQueryWrapper<UserPaidAlbum>()
                .eq(UserPaidAlbum::getUserId, userId));
        //	判断对象是否为空
        if (null != userPaidAlbum) {
            //	说这个用户购买过专辑,需要将需要购买的声音Id 设置为免费; trackId:0;
//            			HashMap<Long, Integer> map = new HashMap<>();
//            			for (Long trackId : trackIdList) {
//            				map.put(trackId,0);
//            			}
//            			return map;
        }
        //	当前这个用户没有购买专辑, 查看是否购买过声音; user_paid_track
        //	查询当前这个用户，是否购买过当前专辑中需要付费的声音列表： select * from user_paid_track where user_id = 28 and album_id = 1429 and track_id in ( 48241,48249);
        LambdaQueryWrapper<UserPaidTrack> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserPaidTrack::getUserId, userId).eq(UserPaidTrack::getAlbumId, albumId)
                .in(UserPaidTrack::getTrackId, trackIdList);

        List<Long> trackIdPaidList = userPaidTrackMapper.selectList(wrapper).stream()
                .map(UserPaidTrack::getTrackId).toList();

        //	声明map 集合
        HashMap<Long, Integer> map = new HashMap<>();
        //	trackIdList -- 需要所有付费的声音Id;  userPaidTrackList 不需要付费的声音Id;
        for (Long trackId : trackIdList) {
            //	判断
            if (trackIdPaidList.contains(trackId)) {
                //	设置免费
                map.put(trackId, 1);
            } else {
                //	设置付费
                map.put(trackId, 0);
            }
        }
        // 返回集合
        return map;

    }
}
